﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ForrajeriaDB; //ESTE USING VA EN TODOS LOS ELEMENTOS DE ESTA CAPA
using System.Data;
using System.Data.SqlClient;
namespace ForrajeriaDAL
{
    public class AnimalXForrajeDAL : AccesoDAL, InterfazDAL<AnimalXForrajeMO>
    {

        private SqlCommand cmd; //var de comando SQL

        #region VARIABLES PARA EL STORED QUE MANEJA EL OBJETO DAL

        private static string insert = "AnimalXForraje_INSERT";
        private static string delete = "AnimalXForraje_DELETE";
        

        #endregion

        #region "Singleton"

        private static AnimalXForrajeDAL instance;

        private AnimalXForrajeDAL()
            : base()
        {
            
        }


        public static AnimalXForrajeDAL Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new AnimalXForrajeDAL();
                }
                return instance;
            }
        }

        #endregion

        #region Interfaz
        public List<AnimalXForrajeMO> GetAll()
        {
            throw new NotImplementedException();
        }

        public void Adicionar(AnimalXForrajeMO item)
        {
            try
            {
                cmd = new SqlCommand(insert, getConexion());
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@codigoAnimal", SqlDbType.VarChar, 50).Value = item.CodigoAnimal;
                cmd.Parameters.Add("@codigoForraje", SqlDbType.VarChar, 50).Value = item.CodigoForraje;

                opencon();
                cmd.ExecuteNonQuery();
                closecon();                 //cierro conexion
                 
                
                
                                
                

            }
            catch (SqlException ex)
            {
                closecon();
                throw ex;
            }


        }

        public void Eliminar(AnimalXForrajeMO item)
        {
            try
            {
                cmd = new SqlCommand(delete, getConexion());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@codigoAnimal", SqlDbType.VarChar, 50).Value = item.CodigoAnimal;
                cmd.Parameters.Add("@codigoForraje", SqlDbType.VarChar, 50).Value = item.CodigoForraje;

                
                opencon();
                cmd.ExecuteNonQuery();
                closecon();
                
            }
            catch (SqlException ex)
            {
                closecon();
                throw ex;
            }
        }

        public void Actualizar(AnimalXForrajeMO nuevoItem, AnimalXForrajeMO origItem)
        {
            throw new NotImplementedException();
        }

        #endregion

    
    }

}
